package com.yuncheng.oaApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.bxdService.OaJcyFybxdService;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.constant.OaFlowConstant;
import com.yuncheng.oaApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.mapper.OaSysFlowPageMapper;
import com.yuncheng.oaApi.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 流程页面
 * @Author: yuncheng
 * @Date: 2023-10-12
 * @Version: V1.0
 */
@Service
public class OaSysFlowPageServiceImpl extends BaseServiceImpl<OaSysFlowPageMapper, OaSysFlowPage> implements IOaSysFlowPageService {


    private static final Logger log = LoggerFactory.getLogger(OaSysFlowPageServiceImpl.class);


    @Resource
    @Lazy
    private IOaSysFlowPageService sysFlowPageService;

    @Resource
    @Lazy
    private IOaSpcySqsxJobService spcySqsxJobService;

    @Resource
    @Lazy
    private IOaSysFlowNodeService sysFlowNodeService;

    @Resource
    @Lazy
    private IOaLgqsService oaLgqsService;

    @Resource
    @Lazy
    private IOaJbzbglService oaJbzbglService;

    @Resource
    @Lazy
    private IOaQxjspService oaQxjspService;

    @Resource
    @Lazy
    private IOaXzswspEntityService oaXzswspService;

    @Resource
    @Lazy
    private IOaXcxxspglService oaXcxxspglService;

    @Resource
    @Lazy
    private IOaHysglService oaHysglService;

    @Resource
    @Lazy
    private IOaBgypsqglService oaBgypsqglService;

    @Resource
    @Lazy
    private IOaBgypsxmxxxService oaBgypsxmxxxService;

    @Resource
    @Lazy
    private IOaSysFlowTempleService sysFlowTempleService;

    @Resource
    @Lazy
    private INkRsYstpsqService nkRsYstpsqService;

    @Resource
    @Lazy
    private INkRsYstpmxService nkRsYstpmxService;

    @Resource
    @Lazy
    private INkRsCcspService nkRsCcspService;

   @Resource
   @Lazy
   private INkRsHtspjlService nkRsHtspjlService;


   @Resource
   @Lazy
   private IPartyRsDyrdsqjlService partyRsDyrdsqjlService;

   @Resource
   @Lazy
   private IQyReservationService qyReservationService;


   @Resource
   @Lazy
   private OaJcyFybxdService oaJcyFybxdService;

   @Resource
   @Lazy
   private IOaSpcyJcyFybxdService spcyJcyFybxdService;

   @Resource
   @Lazy
   private IOaSpcyJcyFybxdtssxsqService spcyJcyFybxdtssxsqService;


   @Resource
   @Lazy
   private IOaProcessBusinessService oaProcessBusinessService;

    //预算部门申报
    @Resource
    @Lazy
    private INkRsBmysxmsbService nkRsBmysxmsbService;

    //预算部门申报-项目内容
    @Resource
    @Lazy
    private INkBmysxmsbXmnrService nkRsBxmnrService;

    //预算部门申报-项目依据
    @Resource
    @Lazy
    private INkBmysxmsbXmyjService nkRsBxmyjService;


    //预算申报
    @Resource
    @Lazy
    private INkBmysxmsbsplcService nkRsBmysxmsbsplcService;

    //经常性事务申请

    @Resource
    @Lazy
    private INkJcxswsqService nkJcxswsqService;




    public Map byJodId(String jobId, String lcmbbm, String fpid) {
        HashMap map = new HashMap();
        Object oaMap = null;
        List<Object> mxList = null;
        String  lcywsqsl = null;
        List nkblrList = new ArrayList<>();

        String lcmbbms="";

        if (StringUtils.isNotBlank(jobId)) {
            OaJobEntity oaJobEntity = spcySqsxJobService.getById(jobId);
            String fnodeid = oaJobEntity.getFnodeid();
            String fhjmc = oaJobEntity.getFhjmc();

            OaSysFlowNode flowNode = sysFlowNodeService.getById(fnodeid);
            if (flowNode == null){
                throw new BusinessException("没有相匹配的流程配置结点！");
            }
            String fymid = flowNode.getFymid();

            OaSysFlowPage pageMap = sysFlowPageService.getById(fymid);
            if (pageMap == null) {
                throw new BusinessException("节点页面配置为空，请完善节点配置！");
            }

            List<OaProcessBusiness> oaProcessBusinesses = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                    .eq(OaProcessBusiness::getFpid, fpid));

            //通过流程模板编码 来判断查询的主表业务
            if (lcmbbm.equals(OaFlowConstant.OA_LGQS)) {
                oaMap = oaLgqsService.getById(fpid);
                lcmbbms = OaFlowConstant.OA_LGQS;

            } else if (lcmbbm.equals(OaFlowConstant.OA_QXJ)) {
                oaMap = oaQxjspService.getById(fpid);
                lcmbbms = OaFlowConstant.OA_QXJ;

            } else if (lcmbbm.equals(OaFlowConstant.OA_JBZB)) {
                oaMap = oaJbzbglService.getById(fpid);
                lcmbbms = OaFlowConstant.OA_JBZB;
            } else if (lcmbbm.equals(OaFlowConstant.OA_XZSWSP)) {
                oaMap = oaXzswspService.getById(fpid);
                lcmbbms = OaFlowConstant.OA_XZSWSP;
            } else if (lcmbbm.equals(OaFlowConstant.OA_XCXXSP)) {
                oaMap = oaXcxxspglService.getById(fpid);
                lcmbbms = OaFlowConstant.OA_XCXXSP;
            } else if (lcmbbm.equals(OaFlowConstant.OA_HYSSQGL)) {
                oaMap = oaHysglService.getById(fpid);
                lcmbbms = OaFlowConstant.OA_HYSSQGL;
            } else if (lcmbbm.equals(OaFlowConstant.OA_BGYPSQ)) {
                oaMap = oaBgypsqglService.getById(fpid);
                lcmbbms = OaFlowConstant.OA_BGYPSQ;
                List<OaBgypsxmxxx> list = oaBgypsxmxxxService.list(new LambdaQueryWrapper<OaBgypsxmxxx>().eq(OaBgypsxmxxx::getFsqid, fpid));
                if (CollectionUtil.isNotEmpty(list)){
                    mxList = list.stream().map(e -> (Object)e).collect(Collectors.toList());
                }

            }else if (lcmbbm.equals(OaFlowConstant.NK_YSTPSQ)){
                oaMap = nkRsYstpsqService.getById(fpid);
                lcmbbms = OaFlowConstant.NK_YSTPSQ;
                List<NkRsYstpmx> list = nkRsYstpmxService.list(new LambdaQueryWrapper<NkRsYstpmx>().eq(NkRsYstpmx::getFystpsqid, fpid));
                if (CollectionUtil.isNotEmpty(list)){
                    mxList = list.stream().map(e -> (Object)e).collect(Collectors.toList());
                }

            }else if (lcmbbm.equals(OaFlowConstant.NK_BXSQLC)){
                oaMap = spcyJcyFybxdService.getById(fpid);
                Map zzryBd = oaJcyFybxdService.getZzryBd(fpid);
                map.put("zzryBd",zzryBd);
                lcmbbms = OaFlowConstant.NK_BXSQLC;



            }else if (lcmbbm.equals(OaFlowConstant.NK_CCSP)){
                oaMap = nkRsCcspService.getById(fpid);
                lcmbbms = OaFlowConstant.NK_CCSP;
            }else if (lcmbbm.equals(OaFlowConstant.NK_HTSPLC)){
                oaMap = nkRsHtspjlService.getById(fpid);
                lcmbbms = OaFlowConstant.NK_HTSPLC;
            }else if (lcmbbm.equals(OaFlowConstant.FZPARTYWORKFLOW)){
                oaMap = partyRsDyrdsqjlService.getById(fpid);
                lcmbbms = OaFlowConstant.FZPARTYWORKFLOW;
            }else if (lcmbbm.equals(OaFlowConstant.QY_YYZXDJ)){
                oaMap = qyReservationService.getById(fpid);
                lcmbbms = OaFlowConstant.QY_YYZXDJ;
            }else if (lcmbbm.equals(OaFlowConstant.BXD_TSSXSQ)){
                oaMap =  spcyJcyFybxdtssxsqService.getById(fpid);
                lcmbbms = OaFlowConstant.BXD_TSSXSQ;
            }else if (lcmbbm.equals(OaFlowConstant.NK_BM_YSSBLC)){
                oaMap =  nkRsBmysxmsbService.getById(fpid);
                lcmbbms = OaFlowConstant.NK_BM_YSSBLC;
                //部门预算申报 明细需要 项目内容 项目依据

//                List<NkBmysxmsbXmnr> nkBmysxmsbXmnrs = nkRsBxmnrService.list(new LambdaQueryWrapper<NkBmysxmsbXmnr>()
//                        .eq(NkBmysxmsbXmnr::getFbmysxmsbid, fpid));
//                List<NkBmysxmsbXmyj> bmysxmsbXmyjList = nkRsBxmyjService.list(new LambdaQueryWrapper<NkBmysxmsbXmyj>()
//                        .eq(NkBmysxmsbXmyj::getFbmysxmsbid, fpid));
//
//
//                if (mxList == null) {
//                    mxList = new ArrayList<>(); // 初始化mxList，避免空指针异常
//                }
//                mxList.addAll(nkBmysxmsbXmnrs);
//                mxList.addAll(bmysxmsbXmyjList);

            }else if(lcmbbm.equals(OaFlowConstant.NK_YSSBLC)){

                oaMap =  nkRsBmysxmsbsplcService.getById(fpid);
                lcmbbms = OaFlowConstant.NK_YSSBLC;

                //将 预算项目申报的数据一起返回
                List<NkRsBmysxmsb> nkRsBmysxmsbs = nkRsBmysxmsbService.list(new LambdaQueryWrapper<NkRsBmysxmsb>()
                        .eq(NkRsBmysxmsb::getFjhid, fpid));
                map.put("yssb", nkRsBmysxmsbs);


            }

            map.put("pageMap", pageMap);
            map.put("oaMap", oaMap);
            map.put("jobMap", oaJobEntity);
            map.put("hjmc", fhjmc);
            map.put("mxList",mxList);
            map.put("lcmbbms",lcmbbms);


        }else {
            if (lcmbbm.equals(OaFlowConstant.OA_LGQS)) {
                oaMap = oaLgqsService.getById(fpid);
                lcmbbm = OaFlowConstant.OA_LGQS;
            } else if (lcmbbm.equals(OaFlowConstant.OA_QXJ)) {
                oaMap = oaQxjspService.getById(fpid);
                lcmbbm = OaFlowConstant.OA_QXJ;
            } else if (lcmbbm.equals(OaFlowConstant.OA_JBZB)) {
                oaMap = oaJbzbglService.getById(fpid);
                lcmbbm = OaFlowConstant.OA_JBZB;
            } else if (lcmbbm.equals(OaFlowConstant.OA_XZSWSP)) {
                oaMap = oaXzswspService.getById(fpid);
                lcmbbm = OaFlowConstant.OA_XZSWSP;
            } else if (lcmbbm.equals(OaFlowConstant.OA_XCXXSP)) {
                oaMap = oaXcxxspglService.getById(fpid);
                lcmbbm = OaFlowConstant.OA_XCXXSP;
            } else if (lcmbbm.equals(OaFlowConstant.OA_HYSSQGL)) {
                oaMap = oaHysglService.getById(fpid);
                lcmbbm = OaFlowConstant.OA_HYSSQGL;
            } else if (lcmbbm.equals(OaFlowConstant.OA_BGYPSQ)) {
                oaMap = oaBgypsqglService.getById(fpid);
                lcmbbm = OaFlowConstant.OA_BGYPSQ;
                List<OaBgypsxmxxx> list = oaBgypsxmxxxService.list(new LambdaQueryWrapper<OaBgypsxmxxx>().eq(OaBgypsxmxxx::getFsqid, fpid));
                mxList = list.stream().map(e -> (Object)e).collect(Collectors.toList());
            }else if (lcmbbm.equals(OaFlowConstant.NK_YSTPSQ)){
                oaMap = nkRsYstpsqService.getById(fpid);
                lcmbbm = OaFlowConstant.NK_YSTPSQ;
            }else if (lcmbbm.equals(OaFlowConstant.NK_BXSQLC)){

                oaMap =spcyJcyFybxdService.getById(fpid);
                Map zzryBd = oaJcyFybxdService.getZzryBd(fpid);
                lcmbbm = OaFlowConstant.NK_BXSQLC;
                map.put("zzryBd",zzryBd);

            }else if (lcmbbm.equals(OaFlowConstant.NK_CCSP)){
                oaMap = nkRsCcspService.getById(fpid);
                lcmbbm = OaFlowConstant.NK_CCSP;
            }else if (lcmbbm.equals(OaFlowConstant.NK_HTSPLC)){
                oaMap = nkRsHtspjlService.getById(fpid);
                lcmbbm = OaFlowConstant.NK_HTSPLC;
            }else if (lcmbbm.equals(OaFlowConstant.FZPARTYWORKFLOW)){
                oaMap = partyRsDyrdsqjlService.getById(fpid);
                lcmbbm = OaFlowConstant.FZPARTYWORKFLOW;
            }else if (lcmbbm.equals(OaFlowConstant.QY_YYZXDJ)){
                oaMap = qyReservationService.getById(fpid);
                lcmbbm = OaFlowConstant.QY_YYZXDJ;
            }else if (lcmbbm.equals(OaFlowConstant.BXD_TSSXSQ)){
                oaMap = spcyJcyFybxdtssxsqService.getById(fpid);
                lcmbbm = OaFlowConstant.BXD_TSSXSQ;
            }else if (lcmbbm.equals(OaFlowConstant.NK_BM_YSSBLC)){
                oaMap = nkRsBmysxmsbService.getById(fpid);
                lcmbbm = OaFlowConstant.NK_BM_YSSBLC;
            }else if (lcmbbm.equals(OaFlowConstant.NK_YSSBLC)){
                oaMap= nkRsBmysxmsbsplcService.getById(fpid);
                lcmbbm = OaFlowConstant.NK_YSSBLC;
            }
//            else if (lcmbbm.equals(OaFlowConstant.NK_JCXSWSQ)){
//                oaMap= nkJcxswsqService.getById(fpid);
//                lcmbbm = OaFlowConstant.NK_JCXSWSQ;
//            }
            //根据流程模板编码获取 模板数据
            List<OaSysFlowTemple> oaSysFlowTemples = sysFlowTempleService.list(new LambdaQueryWrapper<OaSysFlowTemple>().eq(OaSysFlowTemple::getFlcbm, lcmbbm));
            String templesId = oaSysFlowTemples.get(0).getId();
            //根据模板主键id 获取 第一个环节
            List<OaSysFlowNode> firstNode = sysFlowNodeService.getFirstNode(templesId);
            //获取pagemap
            OaSysFlowNode flowNodeServiceById = sysFlowNodeService.getById(firstNode.get(0).getId());
            String fymid = flowNodeServiceById.getFymid();
            OaSysFlowPage pageMap = sysFlowPageService.getById(fymid);

            map.put("pageMap", pageMap);
            map.put("oaMap", oaMap);
            map.put("mxList",mxList);
            map.put("lcmbbm",lcmbbm);
        }

        return map;
    }



    @Override
    public List pidgetlchj(String pid) {
        List jdlist = new ArrayList<>();
        Map nrmap = new HashMap();


        if (StringUtils.isBlank(pid)) {
            throw new BusinessException("流程pid缺失");
        }


        //获取办理状态为待办和退回的节点
        List<OaJobEntity> oaJobEntities = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                .eq(OaJobEntity::getFpid, pid));
        //如果不为空，获取办理状态为待办或者退回的数据记录
        if (CollectionUtil.isNotEmpty(oaJobEntities)) {

            //筛选后如果不为空代表流程未结束
            List<OaJobEntity> filteredOaJobEntities = oaJobEntities.stream()
                    .filter(job -> OaCommonFlowConstant.BLLX_DB.equals(job.getFblzt()) || OaCommonFlowConstant.BLLX_TH.equals(job.getFblzt()))
                    .collect(Collectors.toList());

            //获取当前节点名称 以及所有的上级节点
            if (CollectionUtil.isNotEmpty(filteredOaJobEntities)){
                //当前流程
                OaJobEntity job = filteredOaJobEntities.get(0);

                OaSysFlowNode oaSysFlowNode = sysFlowNodeService.getById(job.getFnodeid());

                // 如果当前节点的上一环节不为空 ，那就代表这个流程不是第一个流程
                if (StringUtils.isNotBlank(oaSysFlowNode.getFsyhjid())){
                    //先加上当前的节点

                    nrmap.put("blzt",job.getFblzt());
                    nrmap.put("blr",job.getFblr());
                    nrmap.put("blrid",job.getFydbrid());
                    nrmap.put("hjmc", oaSysFlowNode.getFjdmc());
                    //增加时间修改。后续做出改变
                    jdlist.add(nrmap);
                    //循环调用获取上一环节节点的方法，直到上一环节节点id为空
                    while (StringUtils.isNotBlank(oaSysFlowNode.getFsyhjid())){
                        List<OaSysFlowNode> previousNode = sysFlowNodeService.getPreviousNode(oaSysFlowNode.getId());
                        if (CollectionUtil.isNotEmpty(previousNode)){
                            List<OaJobEntity> list = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                                    .eq(OaJobEntity::getFpid, pid)
                                    .eq(OaJobEntity::getFnodeid, previousNode.get(0).getId()));
                            if (CollectionUtil.isNotEmpty(list)){
                                OaJobEntity oaJobEntity = list.get(0);
                                Map nrmap3 = new HashMap();
                                nrmap3.put("blzt", oaJobEntity.getFblzt());
                                nrmap3.put("blr", oaJobEntity.getFblr());
                                nrmap3.put("blrid", oaJobEntity.getFydbrid());
                                nrmap3.put("hjmc",previousNode.get(0).getFjdmc());
                                jdlist.add(nrmap3);
                            }else {
//                                Map nrmap2 = new HashMap();
//                                nrmap2.put("blzt","");
//                                nrmap2.put("blr","");
//                                nrmap2.put("blrid","");
//                                nrmap2.put("hjmc",previousNode.get(0).getFjdmc());
//                                jdlist.add(nrmap2);
                            }
                            oaSysFlowNode = previousNode.get(0);
                        }

                    }

                    //如果为空，那就是第一个流程直接返回
                }else {

                    nrmap.put("blzt",job.getFblzt());
                    nrmap.put("blr",job.getFblr());
                    nrmap.put("blrid",job.getFydbrid());
                    nrmap.put("hjmc", oaSysFlowNode.getFjdmc());
                    jdlist.add(nrmap);
                }
                //如果为空，那就代表流程结束，直接返回整个流程
            }else {

                //随便获取流程 然后拿到流程模板id，循环获取节点名称
                List<OaJobEntity> list = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                        .eq(OaJobEntity::getFpid, pid));
                if (CollectionUtil.isNotEmpty(list)){
                    OaJobEntity job = list.get(0);
                    String fnodeid = job.getFnodeid();
                    OaSysFlowNode oaSysFlowNode = sysFlowNodeService.getById(fnodeid);
                    String flcmbid = oaSysFlowNode.getFlcmbid();
                    List<OaSysFlowNode> list1 = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>()
                            .eq(OaSysFlowNode::getFlcmbid, flcmbid));

                    for (OaJobEntity jobs : list){
                        Map nrmap4 = new HashMap();
                        nrmap4.put("blzt",jobs.getFblzt());
                        nrmap4.put("blr",jobs.getFblr());
                        nrmap4.put("blrid",jobs.getFydbrid());
                        nrmap4.put("hjmc",jobs.getFhjmc());
                        jdlist.add(nrmap4);
                    }
                }

            }
        }
        return jdlist;
    }


    /**
     * 修改待办申请时间
     */
    @Override
    public void updatesqsj(String pid,String sqsj){
        if (StringUtils.isBlank(pid)){
            throw new BusinessException("流程id缺失");
        }

        List<OaProcessBusiness> oaProcessBusinesses = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                    .eq(OaProcessBusiness::getFpid, pid));
        if (CollectionUtil.isNotEmpty(oaProcessBusinesses)){
            OaProcessBusiness oaProcessBusiness = oaProcessBusinesses.get(0);
            oaProcessBusiness.setFsqsj(sqsj);
            oaProcessBusinessService.updateById(oaProcessBusiness);

        }

    }








}
